﻿#pragma once

#include "../framework.h"
#include "../ikit/ikit.h"
#include "../resource.h"
#include <dx_window.h>
#include <functional>
#include <map>

#define MAX_LOADSTRING 100
#define DEBUG_MODE TRUE

class WindowApp {
public:
  WindowApp();
  ~WindowApp();

  void Init(HINSTANCE _instance, LPWSTR lpCmdLine, int nCmdShow);
  bool CreateWinForm(const char* title, UINT width, UINT height);
  // 处理事件
  void RollEvents();

  WPARAM GetMsgWParam() const { return msg_.wParam; }
  UINT GetWidth() const { return width_; }
  UINT GetHeight() const { return height_; }
  HWND GetHandle() const { return hwnd_; }
  HFONT GetFont() const { return font_; }
  RECT GetRect() const;
  ifire::DxWindow* GetDxWindow() const { return dx_window_.get(); }
  bool IsExited() const { return app_exited_; }
  void SetTitle(const char* title) const { SetWindowText(hwnd_, title); }
  void RegisterAction(const std::string& name, std::function<void()> action);
  void RegisterCommandHandle(std::function<void(int)> command_handle);
  void ExecuteAction(const std::string& name);
  void ExecuteAction(int index);

  static WindowApp* GetApp() { return win_app_instance_; }
  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
                           LPARAM lParam);

  // 窗口控制
  HWND AddButton(const char* button_name, int button_id, RECT rect);
  HWND CreateLabel(const char* text, RECT rect);
  HWND CreateTextArea(RECT rect);
  void AddTextToEdit(HWND handle, const char* str, bool newLine) const;
  void SetFont(HWND target);
  auto& GetActions() { return actions_; }

private:
  // 此代码模块中包含的函数的前向声明:
  ATOM MyRegisterClass();
  void SetupFont();

  char szTitle[MAX_LOADSTRING];       // 标题栏文本
  char szWindowClass[MAX_LOADSTRING]; // 主窗口类名

  HFONT font_;
  HWND hwnd_;
  MSG msg_;
  HACCEL accel_table_;
  int cmd_show_;
  UINT width_, height_;
  bool app_exited_; // 是否已经退出
  std::unique_ptr<ifire::DxWindow> dx_window_;
  std::map<std::string, std::function<void()>> actions_;
  std::function<void(int)> command_handle_;
  static WindowApp* win_app_instance_;
};